package com.empire.module.member.controller.admin.warninggather;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.warninggather.vo.*;
import com.empire.module.member.dal.dataobject.warninggather.WarningGatherDO;
import com.empire.module.member.service.warninggather.WarningGatherService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 聚集预警主表（1条记录对应1次聚集事件）")
@RestController
@RequestMapping("/member/warning-gather")
@Validated
public class WarningGatherController {

    @Resource
    private WarningGatherService warningGatherService;

    @PostMapping("/create")
    @Operation(summary = "创建聚集预警主表（1条记录对应1次聚集事件）")
    @PreAuthorize("@ss.hasPermission('member:warning-gather:create')")
    public CommonResult<Long> createWarningGather(@Valid @RequestBody WarningGatherSaveReqVO createReqVO) {
        return success(warningGatherService.createWarningGather(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新聚集预警主表（1条记录对应1次聚集事件）")
    @PreAuthorize("@ss.hasPermission('member:warning-gather:update')")
    public CommonResult<Boolean> updateWarningGather(@Valid @RequestBody WarningGatherSaveReqVO updateReqVO) {
        warningGatherService.updateWarningGather(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除聚集预警主表（1条记录对应1次聚集事件）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:warning-gather:delete')")
    public CommonResult<Boolean> deleteWarningGather(@RequestParam("id") Long id) {
        warningGatherService.deleteWarningGather(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得聚集预警主表（1条记录对应1次聚集事件）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:warning-gather:query')")
    public CommonResult<WarningGatherRespVO> getWarningGather(@RequestParam("id") Long id) {
        WarningGatherDO warningGather = warningGatherService.getWarningGather(id);
        return success(BeanUtils.toBean(warningGather, WarningGatherRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得聚集预警主表（1条记录对应1次聚集事件）分页")
    @PreAuthorize("@ss.hasPermission('member:warning-gather:query')")
    public CommonResult<PageResult<WarningGatherRespVO>> getWarningGatherPage(@Valid WarningGatherPageReqVO pageReqVO) {
        PageResult<WarningGatherDO> pageResult = warningGatherService.getWarningGatherPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarningGatherRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出聚集预警主表（1条记录对应1次聚集事件） Excel")
    @PreAuthorize("@ss.hasPermission('member:warning-gather:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarningGatherExcel(@Valid WarningGatherPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarningGatherDO> list = warningGatherService.getWarningGatherPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "聚集预警主表（1条记录对应1次聚集事件）.xls", "数据", WarningGatherRespVO.class,
                        BeanUtils.toBean(list, WarningGatherRespVO.class));
    }

}